package com.mall.order.bootstrap;

import com.alibaba.druid.util.JdbcUtils;
import com.mysql.jdbc.Driver;
import org.junit.Test;

import java.math.BigDecimal;
import java.sql.*;
import java.util.Random;

/**
 * @Author:
 * @description:
 * @keypoint:
 * @tags:
 * @related:
 */

public class BuildData {

    @Test
    public void fakeData() throws SQLException {
        Long[] itemId = {100040607L,100053202L};
        Random random = new Random();
        Connection orderConnection = DriverManager.getConnection("jdbc:mysql://localhost:3306/csmall?useSSL=false&rewriteBatchedStatements=true&useUnicode=true&characterEncoding=utf8","root","123456");

        PreparedStatement orderPreparedStatement = orderConnection.prepareStatement("insert into tb_order (order_id,payment,status,create_time,update_time,user_id) values(?,?,?,?,?,?)");
        PreparedStatement orderItemPreparedStatement = orderConnection.prepareStatement("insert into tb_order_item (id,item_id,order_id,num,title,price,total_fee,status,create_time,update_time) values(?,?,?,?,?,?,?,?,?,?)");
        PreparedStatement orderShippingPreparedStatement = orderConnection.prepareStatement("insert into tb_order_shipping (order_id,receiver_name,receiver_mobile,receiver_state,receiver_city,receiver_district,receiver_address,created,updated) values(?,?,?,?,?,?,?,?,?)");

        for (int i = 0; i < 20; i++) {
            String orderId = "testOrder"+i;
            orderPreparedStatement.setString(1,orderId);
            BigDecimal payment = new BigDecimal(10);
            orderPreparedStatement.setBigDecimal(2,payment);
            orderPreparedStatement.setInt(3, 0);
            orderPreparedStatement.setDate(4, new Date(System.currentTimeMillis()));
            orderPreparedStatement.setDate(5, new Date(System.currentTimeMillis()));
            orderPreparedStatement.setLong(6,74);

            orderPreparedStatement.addBatch();;
            for (int j = 0; j < 2; j++) {
//                String orderItemId = "testOrderItem"+
                orderItemPreparedStatement.setString(1,"testOrderItem"+i+j);
//                int index = random.nextInt(2);
                orderItemPreparedStatement.setLong(2, itemId[j]);
                orderItemPreparedStatement.setString(3,orderId);
                int num = random.nextInt(5)+1;
                orderItemPreparedStatement.setInt(4,num);
                orderItemPreparedStatement.setString(5,"testTitle");
                BigDecimal price = new BigDecimal(200);
                orderItemPreparedStatement.setBigDecimal(6,price);
                BigDecimal multiply = price.multiply(new BigDecimal(num));
                orderItemPreparedStatement.setBigDecimal(7,multiply);
                orderItemPreparedStatement.setInt(8,1);
                orderItemPreparedStatement.setDate(9, (java.sql.Date) new Date(System.currentTimeMillis()));
                orderItemPreparedStatement.setDate(10, (java.sql.Date) new Date(System.currentTimeMillis()));

                orderItemPreparedStatement.addBatch();
            }

            orderShippingPreparedStatement.setString(1,orderId);//order_id
            orderShippingPreparedStatement.setString(2,"testchenye");//name
            orderShippingPreparedStatement.setString(3,"11111098883");//mobile
            orderShippingPreparedStatement.setString(4,"zhejiang");//state
            orderShippingPreparedStatement.setString(5,"wenling");//city
            orderShippingPreparedStatement.setString(6,"zeg");//district
            orderShippingPreparedStatement.setString(7,"cccc");//address
            orderShippingPreparedStatement.setDate(8, (java.sql.Date) new Date(System.currentTimeMillis()));//created
            orderShippingPreparedStatement.setDate(9, (java.sql.Date) new Date(System.currentTimeMillis()));//up
            orderShippingPreparedStatement.addBatch();
        }

        orderPreparedStatement.executeBatch();
        orderItemPreparedStatement.executeBatch();
        orderShippingPreparedStatement.executeBatch();

        orderItemPreparedStatement.close();
        orderPreparedStatement.close();
        orderShippingPreparedStatement.close();

        orderConnection.close();
    }
}
